package commonUtility;


import javax.swing.ImageIcon;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;

public class JPanelImage extends JPanel
{

	private static final long serialVersionUID = 1L;
	private Image _image = null;  //  @jve:decl-index=0:
	private String _absolutePathImage = null;  //  @jve:decl-index=0:
	
	//TODO Field da cancellare usato solo per i test.
//	private String _absolutePathImage = "./media/ultima-cena.jpg";
	
	/**
	 * @return the _absolutePathImage
	 */
	public String getAbsolutePathImage()
	{
		return _absolutePathImage;
	}

	/**
	 * @param absolutePathImage the _absolutePathImage to set
	 */
	public void setAbsolutePathImage(String absolutePathImage)
	{
		if(_absolutePathImage == null || _absolutePathImage.contentEquals(absolutePathImage) == false)
		{
			_image = null;
			_absolutePathImage = absolutePathImage;
			repaint();
		}
	}

	/**
	 * This is the default constructor
	 */
	public JPanelImage()
	{
		super();
		initialize();
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize()
	{
		this.setSize(400, 300);
		this.setLayout(null);
	}

	/*
	 * Metodo che prende in ingresso l'immagine da ridimenzionare e
	 * restituisce un rettangolo con le nuove dimensioni e le nuove
	 * coordinate da passare al metodo drawImage dell'oggetto Graphics.
	 */
	private Rectangle resizeImage(Image image)
	{
		float widthPanel = (float)getWidth();
		float heightPanel = (float)getHeight();
		float widthImage = (float)image.getWidth(null);
		float heightImage = (float)image.getHeight(null);

		float widthOnHeightPanel = widthPanel / heightPanel;
		float widthOnHeightImage = widthImage / heightImage;

		if(widthOnHeightPanel >= 1.0)
		{
			if(widthOnHeightImage >= 1.0)
			{
				if(widthOnHeightImage >= widthOnHeightPanel)
				{
					float factor = widthImage / widthPanel;
					heightImage = heightImage / factor;
					widthImage = widthPanel;

					Rectangle rectangle = new Rectangle((int)widthImage, (int)heightImage);
					rectangle.x = 0;
					rectangle.y = (int)((heightPanel - heightImage) / 2.0); 
					return rectangle;
				}
			}

			float factor = heightImage / heightPanel;
			widthImage = widthImage / factor;

			heightImage = heightPanel;

			Rectangle rectangle = new Rectangle((int)widthImage, (int)heightImage);
			rectangle.x = (int)((widthPanel - widthImage) / 2.0); 
			rectangle.y = 0;
			return rectangle;
		}
		else
		{
			if(widthOnHeightImage < 1.0)
			{
				if(widthOnHeightPanel >= widthOnHeightImage)
				{
					float factor = heightImage / heightPanel;
					widthImage = widthImage / factor;

					heightImage = heightPanel;

					Rectangle rectangle = new Rectangle((int)widthImage, (int)heightImage);
					rectangle.x = (int)((widthPanel - widthImage) / 2.0); 
					rectangle.y = 0;
					return rectangle;
				}
			}

			float factor = widthImage / widthPanel;
			heightImage = heightImage / factor;
			widthImage = widthPanel;

			Rectangle rectangle = new Rectangle((int)widthImage, (int)heightImage);
			rectangle.x = 0;
			rectangle.y = (int)((heightPanel - heightImage) / 2.0); 
			return rectangle;
		}

	}

	public void paintComponent(Graphics g) 
	{
		setOpaque(false);
		
		if(_absolutePathImage != null && _image == null)
		{
			ImageIcon imageIcon = new ImageIcon(_absolutePathImage);

			_image = imageIcon.getImage();
		}

		if(_image != null)
		{
			Rectangle rectangle = resizeImage(_image);
			
			g.drawImage(_image, rectangle.x, rectangle.y, rectangle.width, rectangle.height, null);
		}

	}

}  //  @jve:decl-index=0:visual-constraint="20,24"
